package com.association.service.impl;

import com.association.mapper.DeptMapper;
import com.association.model.entity.Dept;
import com.association.model.entity.User;
import com.association.model.vo.CourierTokenBean;
import com.association.service.DeptService;
import com.association.utils.RequestTokenBeanLocal;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.len.base.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 18244
 */
@Service
public class DeptServiceImpl extends BaseServiceImpl<Dept, String> implements DeptService {

    @Resource
    DeptMapper deptMapper;

    //查询对象
    @Override
    public Dept getById(Long id) {
        return deptMapper.selectById(id);
    }

    //删除对象
    @Override
    public int delete(Long id) {
        return deptMapper.deleteById(id);
    }

    //添加对象
    @Override
    public int insert(Dept dept) {
        return deptMapper.insert(dept);
    }

    //修改对象
    @Override
    public int update(Dept dept) {
        return deptMapper.update(dept,new QueryWrapper<Dept>().eq("id",dept.getId()));
    }

    //查询集合
    @Override
    public List<Dept> getList() {
        CourierTokenBean s = RequestTokenBeanLocal.get();
        List<Dept> dept = getNest(s.getProjectCode());
        return dept;
    }

    public List<Dept> getNest(String pid){
        List<Dept> list = deptMapper.selectList(new QueryWrapper<Dept>().eq("pid",pid));
        if (list.size()!=0){
            list.forEach(res->{
                res.setDept(getNest(String.valueOf(res.getId())));
            });
        }
        return list;
    }
}
